use base64::prelude::BASE64_STANDARD;
use base64::Engine;
use zuc::zuc::Zuc;

pub fn encryption(input: String) -> String {
    let blocks = padding(input);
    let num = blocks.len();

    let key = vec![0u8; 16];
    let init_vec = vec![1u8; 16];
    let mut zuc = Zuc::new(key, init_vec);
    zuc.init();
    let res = zuc.work(num as u32);

    let mut bytes = vec![];
    for (i, k) in res.iter().enumerate() {
        bytes.extend((k ^ blocks[i]).to_be_bytes());
    }
    BASE64_STANDARD.encode(bytes)
}

fn padding(input: String) -> Vec<u32> {
    let mut input = input.as_bytes().to_vec();
    let pad = 4 - (input.len() % 4);
    input.resize(input.len() + pad, pad as u8);
    let mut blocks = vec![];
    for chunk in input.chunks(4) {
        blocks.push(u32::from_be_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]));
    }
    blocks
}